//
//  ILVideoTableViewController.m
//  ILife
//
//  Created by lanou on 15/11/2.
//  Copyright © 2015年 ILife. All rights reserved.
//

#import "ILVideoTableViewController.h"
#import "FMDatabase.h"
#import "ILDailySelectModel.h"
#import "ILVideoTableViewCell.h"
#import "ILDailySelectDetailViewController.h"

@interface ILVideoTableViewController ()

@property(nonatomic,strong)FMDatabase *dataBase;
@property(nonatomic,strong)NSMutableArray *dataArray;


@end

@implementation ILVideoTableViewController

-(NSMutableArray*)dataArray
{
    if (_dataArray==nil) {
        self.dataArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataArray;
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    

    

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.navigationItem.title = @"音乐收藏";

    self.tableView.rowHeight = 100;
    [self.tableView registerClass:[ILVideoTableViewCell class] forCellReuseIdentifier:@"video"];
    
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    
    NSString *filePath = [NSSearchPathForDirectoriesInDomains(9, 1, 1).firstObject stringByAppendingPathComponent:@"ILive_Video.db"];
    
    _dataBase = [FMDatabase databaseWithPath:filePath];
    
    if ([_dataBase open]) {
        FMResultSet *resultSet = [_dataBase executeQueryWithFormat:@"SELECT * FROM Video"];
        while ([resultSet next]) {
            NSString *coverForDetail = [resultSet stringForColumn:@"coverForDetail"];
            NSString *category = [resultSet stringForColumn:@"category"];
            NSString *title = [resultSet stringForColumn:@"title"];
            NSInteger duration = [resultSet intForColumn:@"duration"];
            NSString *coverBlurred = [resultSet stringForColumn:@"coverBlurred"];
            NSString *des = [resultSet stringForColumn:@"des"];
            
            
            ILDailySelectModel *model = [[ILDailySelectModel alloc] init];
            model.coverForDetail = coverForDetail;
            model.title = title;
            model.category = category;
            model.duration = duration;
            model.coverBlurred = coverBlurred;
            model.des = des;
            
            [self.dataArray addObject:model];
        }
        if (_dataArray.count==0) {
            UIAlertController *alertCollect =[UIAlertController alertControllerWithTitle:@"提示" message:@"没有收藏呢，快去收藏一个吧" preferredStyle:(UIAlertControllerStyleAlert)];
            
            UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:nil];
            [alertCollect addAction:okAction];
            [self presentViewController:alertCollect animated:YES completion:nil];

        }
        [resultSet close];
    } else {
        NSLog(@"数据库打开失败");
    }
    
    [self.tableView reloadData];
    
    self.tableView.tableFooterView = [[UIView alloc]init];

    
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ILVideoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"video" forIndexPath:indexPath];
    ILDailySelectModel *model = self.dataArray[indexPath.row];
    [cell configureDataWithModel:model];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ILDailySelectDetailViewController *detailVC = [[ILDailySelectDetailViewController alloc]init];
    detailVC.dataArr = self.dataArray;
    detailVC.index = indexPath.row;
    [self.navigationController pushViewController:detailVC animated:YES];
}




// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}



// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        ILDailySelectModel *model = self.dataArray[indexPath.row];
         [_dataBase executeUpdate:@"DELETE FROM Video Where title = ?",model.title];
        [self.dataArray removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
       //通知
//        NSDictionary *dic = [NSDictionary dictionaryWithObject:model forKey:@"title"];
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"A" object:@"A" userInfo:dic];
        
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
